package com.gxa.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapUtils {
    /**
     * 将对象转换为map
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String,Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String,Object> map = new HashMap<>();
        Class<?> clazz = obj.getClass();
        for (Field field:clazz.getDeclaredFields()){
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            map.put(fieldName,value);
        }
        return map;
    }
    public static Map<String,String> delNullMap(Map map){
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String,String>entry = it.next();
            if(entry.getValue() == null || entry.getValue() == ""){
                it.remove();
            }
        }
        return map;
    }
}
